import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由懒加载
// import Login from '@/components/Login.vue'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login.vue')
// import Home from '@/components/Home.vue'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home.vue')
// import Welcome from '@/components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome.vue')

// import User from '@/components/user/Users.vue'
const User = () => import(/* webpackChunkName: "user_rights_roles" */ '@/components/user/Users.vue')
// import Rights from '@/components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "user_rights_roles" */ '@/components/power/Rights.vue')
// import Roles from '@/components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "user_rights_roles" */ '@/components/power/Roles.vue')

// import Cate from '@/components/goods/Cate.vue'
const Cate = () => import(/* webpackChunkName: "cate_params" */ '@/components/goods/Cate.vue')
// import Params from '@/components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "cate_params" */ '@/components/goods/Params.vue')

// import List from '@/components/goods/List.vue'
const List = () => import(/* webpackChunkName: "list_add" */ '@/components/goods/List.vue')
// import Addgoods from '@/components/goods/Addgoods'
const Addgoods = () => import(/* webpackChunkName: "list_add" */ '@/components/goods/Addgoods')

// import Order from '@/components/order/Order'
const Order = () => import(/* webpackChunkName: "order_report" */ '@/components/order/Order')
// import Report from '@/components/report/Report'
const Report = () => import(/* webpackChunkName: "order_report" */ '@/components/report/Report')
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/', redirect: '/login' },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
      { path: '/goods/add', component: Addgoods },
      { path: '/rights', component: Rights }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to代表要放问的路径
  // from表示从哪个路径来
  if (to.path === '/login') {
    return next()
  }
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
